package com.voxel.sense.infomsg.widget.adapter;

import android.view.View;

import com.example.com.common.utils.L;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.data.bean.ConditionBean;

import java.util.ArrayList;
import java.util.List;

import cyy.example.com.facotry.Factory;

/**
 * 菜单筛选栏的适配器
 * Created by ChenYiYao on 2018/8/10.
 */
public abstract class AbsPickRecyclerAdapter extends RecyclerAdapter<ConditionBean> {

    private boolean mIsSingleSelect = false; //是否单选
    private int current_position = -1;//单选时，用于记录选中项
    private int mMaxCount = -1; //最大选择数量
    private List<ConditionBean> mSeletedDatas = new ArrayList<>();//已选中的数据

    public AbsPickRecyclerAdapter(AdapterListener<ConditionBean> listener) {
        super(listener);
    }

    public AbsPickRecyclerAdapter(List<ConditionBean> conditionBeans, AdapterListener<ConditionBean> listener) {
        super(conditionBeans, listener);
    }

    public AbsPickRecyclerAdapter() {
    }

    @Override
    public void onBindViewHolder(final ViewHolder<ConditionBean> holder, final int position) {
        super.onBindViewHolder(holder, position);
//        增加点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterStatu(position);
                if (mListener != null) {
                    mListener.onItemClick(holder, mDataList.get(position));
                }
            }
        });
    }

    protected void alterStatu(int position) {
        ConditionBean conditionBean = mDataList.get(position);
        if (handlerSatu(conditionBean)) {
            boolean select = !conditionBean.isSelect();
            //改变数据状态
            if (select && (mMaxCount != -1 && mSeletedDatas.size() >= mMaxCount)) {
                //TODO: 已达最大数据
                ToastUtil.showToastLong(Factory.app(), "最多只能选择五家企业");
                return;
            } else if (select) {
                mSeletedDatas.add(conditionBean);

            } else {
                mSeletedDatas.remove(conditionBean);
            }
            conditionBean.setSelect(select);
            notifyItemChanged(position);
            if (mIsSingleSelect) {
                //若是单选，选择新的item后，取消之前的item
                if (current_position != -1 && position != current_position) {
                    //改变之前数据的状态
                    L.d("");
                    ConditionBean oldBea = mDataList.get(current_position);
                    oldBea.setSelect(false);
                    mSeletedDatas.remove(oldBea);
                    notifyItemChanged(current_position);
                }
                current_position = position;
            }
        }
    }

    /**
     * 由具体的实现则
     *
     * @param conditionBean
     * @return
     */
    protected abstract boolean handlerSatu(ConditionBean conditionBean);

    /**
     * 获取选中的数据
     */
    public List<ConditionBean> getSeletedDatas() {
        return mSeletedDatas;
    }


    public void setSeletedDatas(List<ConditionBean> seletedDatas) {
        mSeletedDatas = seletedDatas;
    }

    /**
     * 重置数据状态
     */
    public void reSetStatus() {
        ArrayList list = new ArrayList();
        boolean flag = false;
        for (ConditionBean bean : mDataList) {
            if (bean.isSelect()) {
                bean.setSelect(false);
                flag = true;
            }
        }
        mSeletedDatas.clear();
        if (flag) {
            notifyDataSetChanged();
        }
    }

    public boolean isSingleSelect() {
        return mIsSingleSelect;
    }

    public void setSingleSelect(boolean singleSelect) {
        mIsSingleSelect = singleSelect;
    }

    public int getMaxCount() {
        return mMaxCount;
    }

    public void setMaxCount(int maxCount) {
        mMaxCount = maxCount;
    }
}
